# Player.gd - 主角控制器
# 《光影之间》游戏主角脚本
extends CharacterBody2D

# 移动参数
@export var speed := 150.0
@export var jump_velocity := -500.0
@export var acceleration := 950.0
@export var friction := 600.0

# 影视能力参数
@export var shadow_vision_energy := 100.0
@export var shadow_vision_max_energy := 100.0
@export var shadow_vision_drain_rate := 20.0
@export var shadow_vision_regen_rate := 10.0

# 状态变量
var is_shadow_vision_active := false
var facing_direction := 1  # 1 = 右, -1 = 左
var current_state := "idle"
var last_state := "idle"

# 节点引用
@onready var sprite := $Sprite2D
@onready var animation_player := $AnimationPlayer
# 影视效果现在由关卡管理，不再在Player中处理

# 重力
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")

# 信号
signal shadow_vision_toggled(is_active: bool)
signal energy_changed(current_energy: float, max_energy: float)
signal state_changed(new_state: String)
signal player_moved
signal player_jumped

func _ready():
	# 连接信号
	shadow_vision_toggled.connect(_on_shadow_vision_toggled)
	
	# 初始状态
	change_state("idle")
	# 播放默认动画
	animation_player.play("default/idle")

func _physics_process(delta):
	# 处理重力
	if not is_on_floor():
		velocity.y += gravity * delta
	
	# 处理跳跃
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = jump_velocity
		change_state("jump")
		player_jumped.emit()
	
	# 处理水平移动
	handle_horizontal_movement(delta)
	
	# 处理影视能力
	handle_shadow_vision(delta)
	
	# 更新动画状态
	update_animation_state()
	
	# 移动角色
	move_and_slide()

func handle_horizontal_movement(delta):
	var direction = Input.get_axis("move_left", "move_right")
	
	if direction != 0:
		# 更新面向方向
		if direction > 0:
			facing_direction = 1
		else:
			facing_direction = -1
		
		# 应用加速度
		velocity.x = move_toward(velocity.x, direction * speed, acceleration * delta)
		
		# 发射移动信号
		player_moved.emit()
		
		# 更新状态
		if is_on_floor():
			if abs(velocity.x) > speed * 0.7:
				change_state("run")
			else:
				change_state("walk")
	else:
		# 应用摩擦力
		velocity.x = move_toward(velocity.x, 0, friction * delta)
		
		# 更新状态
		if is_on_floor() and abs(velocity.x) < 50:
			change_state("idle")

func handle_shadow_vision(delta):
	# 切换影视能力
	if Input.is_action_just_pressed("shadow_vision") and shadow_vision_energy > 0:
		is_shadow_vision_active = not is_shadow_vision_active
		shadow_vision_toggled.emit(is_shadow_vision_active)
	
	# 处理能量消耗和恢复
	if is_shadow_vision_active:
		shadow_vision_energy -= shadow_vision_drain_rate * delta
		if shadow_vision_energy <= 0:
			shadow_vision_energy = 0
			is_shadow_vision_active = false
			shadow_vision_toggled.emit(false)
	else:
		shadow_vision_energy = min(shadow_vision_energy + shadow_vision_regen_rate * delta, shadow_vision_max_energy)
	
	# 发送能量变化信号
	energy_changed.emit(shadow_vision_energy, shadow_vision_max_energy)

func update_animation_state():
	# 更新精灵朝向
	sprite.scale.x = abs(sprite.scale.x) * facing_direction
	
	# 根据状态播放动画
	if current_state != last_state:
		match current_state:
			"idle":
				animation_player.play("default/idle")
			"walk":
				animation_player.play("default/walk")
			"run":
				animation_player.play("default/run")
			"jump":
				if velocity.y < 0:
					animation_player.play("default/jump_up")
				else:
					animation_player.play("default/jump_down")
			"hurt":
				animation_player.play("default/hurt")
		
		last_state = current_state

func change_state(new_state: String):
	if new_state != current_state:
		current_state = new_state
		state_changed.emit(new_state)

# 动画已通过预制资源文件配置，无需代码设置

func _on_shadow_vision_toggled(is_active: bool):
	# 通知游戏管理器
	if GameManager:
		GameManager.on_shadow_vision_changed(is_active)

# 受伤函数
func take_damage(damage: int = 1):
	change_state("hurt")
	# 这里可以添加生命值处理逻辑

# 获取当前状态
func get_current_state() -> String:
	return current_state

# 获取影视能力状态
func get_shadow_vision_energy() -> float:
	return shadow_vision_energy

func is_shadow_vision_enabled() -> bool:
	return is_shadow_vision_active
